/**
 * 学生模型
 */
package dms.model.role;

import dms.model.Classes;
import dms.model.Dormitory;
import dms.model.Faculty;
import dms.model.School;

/**
 * @author 李健
 *
 */
public class Student {

	//实体数据
	private Long id;
	private Account account;	
	private Dormitory dormitory;
	private School school;
	private Faculty faculty;
	private Character gender;	
	private Classes classes;	
	private String studentNumber;
	private String realName;
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}
	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}
	/**
	 * @return the dormitory
	 */
	public Dormitory getDormitory() {
		return dormitory;
	}
	/**
	 * @param dormitory the dormitory to set
	 */
	public void setDormitory(Dormitory dormitory) {
		this.dormitory = dormitory;
	}
	/**
	 * @return the school
	 */
	public School getSchool() {
		return school;
	}
	/**
	 * @param school the school to set
	 */
	public void setSchool(School school) {
		this.school = school;
	}
	/**
	 * @return the faculty
	 */
	public Faculty getFaculty() {
		return faculty;
	}
	/**
	 * @param faculty the faculty to set
	 */
	public void setFaculty(Faculty faculty) {
		this.faculty = faculty;
	}
	/**
	 * @return the gender
	 */
	public Character getGender() {
		return gender;
	}
	/**
	 * @param gender the gender to set
	 */
	public void setGender(Character gender) {
		this.gender = gender;
	}
	/**
	 * @return the classes
	 */
	public Classes getClasses() {
		return classes;
	}
	/**
	 * @param classes the classes to set
	 */
	public void setClasses(Classes classes) {
		this.classes = classes;
	}
	/**
	 * @return the studentNumber
	 */
	public String getStudentNumber() {
		return studentNumber;
	}
	/**
	 * @param studentNumber the studentNumber to set
	 */
	public void setStudentNumber(String studentNumber) {
		this.studentNumber = studentNumber;
	}
	/**
	 * @return the realName
	 */
	public String getRealName() {
		return realName;
	}
	/**
	 * @param realName the realName to set
	 */
	public void setRealName(String realName) {
		this.realName = realName;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
	
/*	//学生要用的仓储
	private RepairRepository repairRepository = null;
	private PurchaseItemRepository purchaseItemRepository = null;
	private DormitoryChangeRepository dormitoryChangeRepository = null;
	private SystemAdministratorRepository systemAdministratorRepository = null;
	private BuildingRepository buildingRepository = null;
	private DormitoryRepository dormitoryRepository = null;
	private StudentRepository studentRepository = null;
	
	*//**
	 * @throws Exception *****************************************************************************************//*
	// Domain operation 
	
	public Repair addRepair(RepairDTO repairInfo) throws Exception {
		Repair repair = new Repair();
		Building building = this.buildingRepository.findById(repairInfo.getBuilding());
		Dormitory dormitory = this.dormitoryRepository.findById(repairInfo.getDormitory());
		DormSupervisor dormSupervisor = this.systemAdministratorRepository.findDormSupervisorById(repairInfo.getDormSupervisor());
		
		repair.setReason(repairInfo.getReason());
		repair.setBuilding(building);
		repair.setDormitory(dormitory);
		repair.setDormSupervisor(dormSupervisor);
		repair.setRequestDate(new Date());
		repair.setStudent(this);
		return this.repairRepository.createRepair(repair);
	}
	
	public Repair updateRepair(RepairDTO repairInfo) throws Exception {
		
		Repair repair = this.repairRepository.findById(repairInfo.getId());
		if (repair.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("表单已受理不能修改");
		}
		Building building = this.buildingRepository.findById(repairInfo.getBuilding());
		Dormitory dormitory = this.dormitoryRepository.findById(repairInfo.getDormitory());
		DormSupervisor dormSupervisor = this.systemAdministratorRepository.findDormSupervisorById(repairInfo.getDormSupervisor());
		
		repair.setReason(repairInfo.getReason());
		repair.setBuilding(building);
		repair.setDormitory(dormitory);
		repair.setDormSupervisor(dormSupervisor);
		repair.setRequestDate(new Date());
		repair.setStudent(this);
		return this.repairRepository.updateRepair(repair);
	}
	
	public void deleteRepairById(Long id) throws Exception {
		Repair repair = this.repairRepository.findById(id);
		if (!repair.getStudent().getId().equals(this.getId())) {
			throw new NoPermissionException("不能删除，不是你申请的维修单");
		}
		if (repair.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("表单已受理不能删除");
		}
		
		this.repairRepository.deleteRepairById(id);
	}
	
	public void deleteRepairs(List<Long> ids) throws Exception {
		Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			Repair repair = this.repairRepository.findById(id);
			
			if (!repair.getStudent().getId().equals(this.getId())) {
				throw new NoPermissionException("不能删除，不是你申请的维修单");
			}
			if (repair.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("表单已受理不能删除");
			}
		}
		
		this.repairRepository.deleteRepairs(ids);
	}
	*//**
	 * 查找时间段的该生寝室repair
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @deprecated
	 *//*
	public PageIterator<Repair> findRepair(Date fromDate, Date toDate) {
		return null;
	}
	*//**
	 * 查找该生寝室所有repair
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @deprecated
	 *//*
	public PageIterator<Repair> findRepair() {
		return findRepair(null, null);
	}
	
	public PurchaseItem addPurchaseItem(PurchaseItemDTO purchaseItemInfo) throws Exception {
		PurchaseItem purchaseItem = new PurchaseItem();
		
		purchaseItem.setContactTel(purchaseItemInfo.getContactTel());
		purchaseItem.setDescription(purchaseItemInfo.getDescription());
		purchaseItem.setDormitory(this.getDormitory());
		purchaseItem.setRequestDate(new Date());
		purchaseItem.setStudent(this);
		
		return this.purchaseItemRepository.createPurchaseItem(purchaseItem);
		
	}
	
	public PurchaseItem updatePurchaseItem(PurchaseItemDTO purchaseItemInfo) throws Exception {
		PurchaseItem purchaseItem = this.purchaseItemRepository.findById(purchaseItemInfo.getId());
		
		if (!purchaseItem.getStudent().getId().equals(this.getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		if (purchaseItem.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("购买申请已受理，不能修改");
		}
		Dormitory dormitory = this.dormitoryRepository.findById(purchaseItemInfo.getDormitory());
		
		purchaseItem.setContactTel(purchaseItemInfo.getContactTel());
		purchaseItem.setDescription(purchaseItemInfo.getDescription());
		purchaseItem.setDormitory(this.getDormitory());

		purchaseItem.setStudent(this);
		
		return this.purchaseItemRepository.updatePurchaseItem(purchaseItem);
	}
	
	public void deletePurchaseItems(List<Long> ids) throws Exception {
		java.util.Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			PurchaseItem purchaseItem = this.purchaseItemRepository.findById(id);
			
			if (!purchaseItem.getStudent().getId().equals(this.getId())) {
				throw new NoPermissionException("你没有权限进行修改");
			}
			if (purchaseItem.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("购买申请已受理，不能删除");
			}
		}
		
		this.purchaseItemRepository.deletePurchaseItems(ids);
	}
	
	public void deletePurchaseItemById(Long id) throws Exception {
		PurchaseItem purchaseItem = this.purchaseItemRepository.findById(id);
		
		if (!purchaseItem.getStudent().getId().equals(this.getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		
		if (purchaseItem.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("购买申请已受理，不能删除");
		}
		
		this.purchaseItemRepository.deletePurchaseItemById(id);
	}
	*//**
	 * @deprecated
	 *//*
	public PageIterator<PurchaseItem> findPurchaseItem(Date fromDate, Date toDate) {
		return null;
	}
	
	public DormitoryChange addDormitoryChange(DormitoryChangeDTO dormitoryChangeInfo) throws Exception {
		DormitoryChange dormitoryChange = new DormitoryChange();
		Dormitory dormitory = this.dormitoryRepository.findById(dormitoryChangeInfo.getDormitory());
		
		dormitoryChange.setDormitory(dormitory);
		dormitoryChange.setReason(dormitoryChangeInfo.getReason());
		dormitoryChange.setRequestDate(new Date());
		dormitoryChange.setStudent(this);
		
		return this.dormitoryChangeRepository.createDormitoryChange(dormitoryChange);
	}
	
	public DormitoryChange updateDormitoryChange(DormitoryChangeDTO dormitoryChangeInfo) throws Exception {
		DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(dormitoryChangeInfo.getId());
		Dormitory dormitory = this.dormitoryRepository.findById(dormitoryChangeInfo.getDormitory());
		
		if (!dormitoryChange.getStudent().getId().equals(this.getId())) {
			throw new NoPermissionException("你没有权限修改该申请");
		}
		
		if (dormitoryChange.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("更换申请已受理，不能更新");
		}
		
		dormitoryChange.setDormitory(dormitory);
		dormitoryChange.setReason(dormitoryChangeInfo.getReason());
		dormitoryChange.setStudent(this);
		
		return this.dormitoryChangeRepository.updateDormitoryChange(dormitoryChange);
	}
	
	public void deleteDormitoryChangeById(Long id) throws Exception {
		DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(id);
		
		if (!dormitoryChange.getStudent().getId().equals(this.getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		if (dormitoryChange.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("申请已受理，不能更新");
		}
		this.dormitoryChangeRepository.deleteDormitoryChangeById(id);
	}
	
	*//**
	 * 
	 * @param ids
	 * @throws Exception
	 *//*
	public void deleteDormitoryChanges(List<Long> ids) throws Exception {
		Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(id);
			
			if (!dormitoryChange.getStudent().getId().equals(this.getId())) {
				throw new NoPermissionException("你没有权限进行修改");
			}
			if (dormitoryChange.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("申请已受理，不能更新");
			}
		}
		
		this.dormitoryChangeRepository.deleteDormitoryChanges(ids);
	}
	
	@Deprecated
	public PageIterator<DormitoryChange> findDormitoryChange(Date fromDate, Date toDate) {
		return null;
	}
	
	*//**
	 * 学生修改自己信息，只修改邮件，或密码
	 * @param studentInfo
	 * @return
	 * @throws NoPermissionException 
	 *//*
	public Student modifyPersonalInfo(StudentDTO studentInfo) throws NoPermissionException {
	
		if (!studentInfo.getId().equals(this.getId())) {
			throw new NoPermissionException("没有权限");
		}
		if (studentInfo.getEmail() != null && studentInfo.getOldEmail() != null) {
			if (this.getAccount().getEmail().equals(
					studentInfo.getOldEmail())) {
				this.getAccount().setEmail(studentInfo.getEmail());
			}
		}
		
		if (studentInfo.getPassword() != null && studentInfo.getOldPassword() != null) {
			if (this.getAccount().getPassword().equals(
					studentInfo.getOldPassword())) {
				this.getAccount().setPassword(studentInfo.getPassword());
			}
		}
		
		if (studentInfo.getTelephone() != null) {
			this.getAccount().setTelephone(studentInfo.getTelephone());
		}
		this.getAccount().setModifyDate(new Date());
		return this.studentRepository.updateStudent(this);
	}
	*/
	/*******************************************************************************************/
	// getter setter method 
	
	/**
	 * @return the account
	 *//*
	public Account getAccount() {
		return account;
	}

	*//**
	 * @param account the account to set
	 *//*
	public void setAccount(Account account) {
		this.account = account;
	}

	*//**
	 * @return the dormitory
	 *//*
	public Dormitory getDormitory() {
		return dormitory;
	}

	*//**
	 * @param dormitory the dormitory to set
	 *//*
	public void setDormitory(Dormitory dormitory) {
		this.dormitory = dormitory;
	}

	*//**
	 * @return the school
	 *//*
	public School getSchool() {
		return school;
	}

	*//**
	 * @param school the school to set
	 *//*
	public void setSchool(School school) {
		this.school = school;
	}

	*//**
	 * @return the faculty
	 *//*
	public Faculty getFaculty() {
		return faculty;
	}

	*//**
	 * @param faculty the faculty to set
	 *//*
	public void setFaculty(Faculty faculty) {
		this.faculty = faculty;
	}

	*//**
	 * @return the gender
	 *//*
	public Character getGender() {
		return gender;
	}

	*//**
	 * @param gender the gender to set
	 *//*
	public void setGender(Character gender) {
		this.gender = gender;
	}

	*//**
	 * @return the classes
	 *//*
	public Classes getClasses() {
		return classes;
	}

	*//**
	 * @param classes the classes to set
	 *//*
	public void setClasses(Classes classes) {
		this.classes = classes;
	}

	*//**
	 * @return the studentNumber
	 *//*
	public String getStudentNumber() {
		return studentNumber;
	}

	*//**
	 * @param studentNumber the studentNumber to set
	 *//*
	public void setStudentNumber(String studentNumber) {
		this.studentNumber = studentNumber;
	}

	*//**
	 * 
	 *//*
	public Student() {
		super();
	}


	*//**
	 * @return the repairRepository
	 *//*
	public RepairRepository getRepairRepository() {
		return repairRepository;
	}

	*//**
	 * @param repairRepository the repairRepository to set
	 *//*
	public void setRepairRepository(RepairRepository repairRepository) {
		this.repairRepository = repairRepository;
	}

	*//**
	 * @return the purchaseItemRepository
	 *//*
	public PurchaseItemRepository getPurchaseItemRepository() {
		return purchaseItemRepository;
	}

	*//**
	 * @param purchaseItemRepository the purchaseItemRepository to set
	 *//*
	public void setPurchaseItemRepository(
			PurchaseItemRepository purchaseItemRepository) {
		this.purchaseItemRepository = purchaseItemRepository;
	}

	*//**
	 * @return the dormitoryChangeRepository
	 *//*
	public DormitoryChangeRepository getDormitoryChangeRepository() {
		return dormitoryChangeRepository;
	}

	*//**
	 * @param dormitoryChangeRepository the dormitoryChangeRepository to set
	 *//*
	public void setDormitoryChangeRepository(
			DormitoryChangeRepository dormitoryChangeRepository) {
		this.dormitoryChangeRepository = dormitoryChangeRepository;
	}

	*//**
	 * @return the systemAdministratorRepository
	 *//*
	public SystemAdministratorRepository getSystemAdministratorRepository() {
		return systemAdministratorRepository;
	}

	*//**
	 * @param systemAdministratorRepository the systemAdministratorRepository to set
	 *//*
	public void setSystemAdministratorRepository(
			SystemAdministratorRepository systemAdministratorRepository) {
		this.systemAdministratorRepository = systemAdministratorRepository;
	}

	*//**
	 * @return the id
	 *//*
	public Long getId() {
		return id;
	}

	*//**
	 * @param id the id to set
	 *//*
	public void setId(Long id) {
		this.id = id;
	}

	 (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	 (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 
	@Override
	public boolean equals(Object obj) {
		System.err.println("enter in");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	*//**
	 * @return the buildingRepository
	 *//*
	public BuildingRepository getBuildingRepository() {
		return buildingRepository;
	}

	*//**
	 * @param buildingRepository the buildingRepository to set
	 *//*
	public void setBuildingRepository(BuildingRepository buildingRepository) {
		this.buildingRepository = buildingRepository;
	}

	*//**
	 * @return the dormitoryRepository
	 *//*
	public DormitoryRepository getDormitoryRepository() {
		return dormitoryRepository;
	}

	*//**
	 * @param dormitoryRepository the dormitoryRepository to set
	 *//*
	public void setDormitoryRepository(DormitoryRepository dormitoryRepository) {
		this.dormitoryRepository = dormitoryRepository;
	}

	*//**
	 * @param repairRepository
	 * @param purchaseItemRepository
	 * @param dormitoryChangeRepository
	 * @param systemAdministratorRepository
	 * @param buildingRepository
	 * @param dormitoryRepository
	 * @param studentRepository
	 *//*
	public Student(RepairRepository repairRepository,
			PurchaseItemRepository purchaseItemRepository,
			DormitoryChangeRepository dormitoryChangeRepository,
			SystemAdministratorRepository systemAdministratorRepository,
			BuildingRepository buildingRepository,
			DormitoryRepository dormitoryRepository,
			StudentRepository studentRepository) {
		super();
		this.repairRepository = repairRepository;
		this.purchaseItemRepository = purchaseItemRepository;
		this.dormitoryChangeRepository = dormitoryChangeRepository;
		this.systemAdministratorRepository = systemAdministratorRepository;
		this.buildingRepository = buildingRepository;
		this.dormitoryRepository = dormitoryRepository;
		this.studentRepository = studentRepository;
	}

	*//**
	 * @return the studentRepository
	 *//*
	public StudentRepository getStudentRepository() {
		return studentRepository;
	}

	*//**
	 * @param studentRepository the studentRepository to set
	 *//*
	public void setStudentRepository(StudentRepository studentRepository) {
		this.studentRepository = studentRepository;
	}

	*//**
	 * @return the realName
	 *//*
	public String getRealName() {
		return realName;
	}

	*//**
	 * @param realName the realName to set
	 *//*
	public void setRealName(String realName) {
		this.realName = realName;
	}

	*/
	
	
	
	
	
	
}
